package com.charm.restaurant.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.charm.restaurant.common.Result;
import com.charm.restaurant.dto.LoginDTO;
import com.charm.restaurant.entity.Merchant;
import com.charm.restaurant.entity.User;
import com.charm.restaurant.service.MerchantService;
import com.charm.restaurant.vo.MerchantVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * 商家控制器
 */
@Slf4j
@RestController
@RequestMapping("/merchant")
public class MerchantController {

    @Autowired
    private MerchantService merchantService;

    /**
     * 商家登录
     */
    @PostMapping("/login")
    public Result<MerchantVO> login(@RequestBody LoginDTO loginDTO) {
        log.info("商家登录：{}", loginDTO.getUsername());
        return merchantService.login(loginDTO);
    }

    /**
     * 商家注册
     */
    @PostMapping("/register")
    public Result<MerchantVO> register(@RequestBody Merchant merchant) {
        log.info("商家注册：{}", merchant.getUsername());
        return merchantService.register(merchant);
    }

    /**
     * 获取商家信息
     */
    @GetMapping("/info")
    public Result<MerchantVO> info() {
        return merchantService.getMerchantInfo();
    }

    /**
     * 商家退出登录
     */
    @PostMapping("/logout")
    public Result<String> logout() {
        return Result.success("退出成功");
    }

    /**
     * 修改商家信息
     */
    @PutMapping("/info")
    public Result<MerchantVO> updateInfo(@RequestBody Merchant merchant) {
        return merchantService.updateInfo(merchant);
    }

    /**
     * 修改密码
     */
    @PutMapping("/password")
    public Result<String> updatePassword(@RequestBody Map<String, String> passwordMap) {
        log.info("商家修改密码请求");

        String oldPassword = passwordMap.get("oldPassword");
        String newPassword = passwordMap.get("newPassword");

        if (oldPassword == null || newPassword == null) {
            return Result.error("密码参数不能为空");
        }

        return merchantService.updatePassword(oldPassword, newPassword);
    }

    /**
     * 获取商家统计数据
     */
    @GetMapping("/statistics")
    public Result<Map<String, Object>> getStatistics() {
        return merchantService.getStatistics();
    }

    /**
     * 分页查询用户列表
     */
    @GetMapping("/users")
    public Result<Page<User>> getUserPage(int page, int pageSize, String username, String name, String phone) {
        log.info("商家查询用户列表，page={}, pageSize={}, username={}, name={}, phone={}",
                page, pageSize, username, name, phone);
        return merchantService.getUserPage(page, pageSize, username, name, phone);
    }

    /**
     * 更新用户状态
     */
    @PutMapping("/users/{id}/status")
    public Result<String> updateUserStatus(@PathVariable Long id, @RequestBody Map<String, Integer> statusMap) {
        log.info("商家更新用户状态，用户ID：{}", id);
        Integer status = statusMap.get("status");
        if (status == null) {
            return Result.error("状态参数不能为空");
        }
        return merchantService.updateUserStatus(id, status);
    }

    /**
     * 获取用户详情
     */
    @GetMapping("/users/{id}")
    public Result<User> getUserDetail(@PathVariable Long id) {
        log.info("商家查询用户详情，用户ID：{}", id);
        return merchantService.getUserDetail(id);
    }
}
